// component/common/pickerMulti/pickerMulti.js
const app = getApp();
Component({
  /**
   * 组件的配置
   */
  options: {
    styleIsolation: 'isolated',
    pureDataPattern: /^_/
  },

  /**
   * 组件的传入样式
   */
  externalClasses: ['style-extra'],

  /**
   * 组件的属性列表
   */
  properties: {
    pickerSelect: {
      type: Array,
      value: [0, 0, 0]
    },
    pickerList: {
      type: Array,
      value: []
    },
    isCustomHeader: {
      type: String,
      value: '0',
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    _bgColor: ['#f0f0f0', '#f8f8f8', '#fff'],
    bgColor: []
  },

  /**
   * 组件的数据监听
   */
  observers: {
    'pickerList': function (pickerList) {
      let t = this;
      t.setData({
        bgColor: t.data._bgColor.slice(t.data._bgColor.length - pickerList.length)
      })
    }
  },

  /**
   * 组件的生命周期
   */
  lifetimes: {
    attached: function () {
      let t = this
      t.setData({
        height: app.globalData.navHeight,
      })
    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onClickPickerRow(e) {
      let t = this
      let {
        row,
        column
      } = e.currentTarget.dataset
      if (t.data.pickerSelect[column] instanceof Array) {
        if (row === 0) {
          t.data.pickerSelect[column] = [0]
        } else {
          if (t.data.pickerSelect[column].indexOf(0) !== -1) {
            t.data.pickerSelect[column].splice(t.data.pickerSelect[column].indexOf(0), 1)
          }
          if (t.data.pickerSelect[column].indexOf(row) !== -1) {
            t.data.pickerSelect[column].splice(t.data.pickerSelect[column].indexOf(row), 1)
          } else {
            t.data.pickerSelect[column].push(row)
          }
        }
      }
      if (typeof t.data.pickerSelect[column] === 'number') {
        t.data.pickerSelect[column] = row
      }
      t.setData({
        pickerSelect: t.data.pickerSelect
      })
      t.triggerEvent('clickRow', t.data.pickerSelect)
    },
    onReset() {
      let t = this
      t.setData({
        pickerSelect: [0, 0, 0]
      })
    },
    onConfirm() {
      let t = this
      let {
        pickerSelect,
        pickerList
      } = t.data
      let lastIndex = pickerList.length - 1
      let select = pickerSelect[lastIndex]
      let list = pickerList[lastIndex]
      let result = []
      if (select instanceof Array) {
        select.forEach(item => {
          result.push(list[item])
        })
      }
      if (typeof select === 'number') {
        result[0] = list[select]
      }
      t.triggerEvent('clickConfirm', {
        result,
        select: pickerSelect,
      })
      t.onClickMask()
    },
    onClickMask() {
      let t = this
      t.triggerEvent('clickMask')
    },
  }
})